package com.aso114.project.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseApplication;
import com.aso114.project.base.BasesAdapter;
import com.aso114.project.bean.QuestionBean;
import com.aso114.project.util.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Error_item_adapter extends BasesAdapter<Error_item_adapter.ViewHolder> implements View.OnClickListener{

    private Error_item_adapter.OnItemClickListener mListener;

    private ArrayList<QuestionBean.QuestionBaseListBean.questionListBean> questionList = new ArrayList<>();

    public Error_item_adapter(Context context) {
        super(context);
    }

    @Override
    public Error_item_adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Error_item_adapter.ViewHolder(getInflater().inflate(R.layout.mycollect_item, parent, false));
    }

    @Override
    public void onBindViewHolder(Error_item_adapter.ViewHolder holder, final int position) {
        holder.myCollectTitle.setText("(" + questionList.get(position).getRealPaperTitle() + ")");
        String title = questionList.get(position).getFormatContent().replace("<p>", "");
        String content = title.replace("</p>", "");
        for (int i = 0; i < questionList.get(position).getQuestioncontentList().size(); i++) {
            content += "\n\n" + questionList.get(position).getQuestioncontentList().get(i).getSelectKey() + "       " + questionList.get(position).getQuestioncontentList().get(i).getSelectValue();
        }
        holder.myCollectContent.setText(content);


        holder.myCollectItemLy.setTag(position);
        holder.myCollectClear.setTag(position);


    }

    @Override
    public int getItemCount() {
        return questionList.size();
    }

    public void setData(ArrayList<QuestionBean.QuestionBaseListBean.questionListBean> questionList) {
        this.questionList = questionList;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.my_collect_title)
        TextView myCollectTitle;
        @BindView(R.id.my_collect_clear)
        ImageView myCollectClear;
        @BindView(R.id.my_collect_item_ly)
        LinearLayout myCollectItemLy;
        @BindView(R.id.my_collect_content)
        TextView myCollectContent;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            myCollectItemLy.setOnClickListener(Error_item_adapter.this);


           myCollectClear.setOnClickListener(Error_item_adapter.this);
        }
    }

//    public void setOnItemClickListener(Error_item_adapter.OnItemClickListener listener) {
//        mListener = listener;
//    }

//    public interface OnItemClickListener {
//        void onItemClick(int position);
//    }


    //=======================以下为item中的button控件点击事件处理===================================

    //item里面有多个控件可以点击（item+item内部控件）
    public enum ViewName {
        ITEM,
        PRACTISE
    }

    //自定义一个回调接口来实现Click和LongClick事件
    public interface OnItemClickListener  {
        void onItemClick(View v, ViewName viewName, int position);
        void onItemLongClick(View v);
    }

    private OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并传给外面的使用者
    public void setOnItemClickListener(OnItemClickListener  listener) {
        this.mOnItemClickListener  = listener;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();      //getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (v.getId()){
//                case R.id.rv_recyclerView:
//                    mOnItemClickListener.onItemClick(v, ViewName.PRACTISE, position);
//                    break;
                default:
                    mOnItemClickListener.onItemClick(v, ViewName.ITEM, position);
                    break;
            }
        }
    }



}
